package com.qhg.saas_server.controller;

import com.github.pagehelper.PageInfo;
import com.qhg.saas_server.empty.ReqQueryProduct;
import com.qhg.saas_server.empty.Result;
import com.qhg.saas_server.pojo.Brand;
import com.qhg.saas_server.pojo.Product;
import com.qhg.saas_server.service.ProductManagerService;
import org.apache.logging.log4j.message.ReusableMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *设置模块-商品管理模块
 */
@CrossOrigin
@RestController
@RequestMapping("/ProductManager")
public class ProductManagerController {
    @Autowired
    private ProductManagerService productManagerService;

    @GetMapping("/test")
    public String test(){
        return "test";
    }

    //品牌信息管理
    //分页查询所有品牌列表 --弃用
//    @GetMapping("/queryAllBrand")
//    public Result queryAllBrand(){
//        return Result.success(productManagerService.queryAllBrand());
//    }
    //模糊查询品牌列表,
    // 接收参数：
    // 查询依据：brandName:品牌名
    // 分页依据：pageNum:当前页码,pageSize:每页记录数,pages:总页数
    // sql limit: limit(pageNum-1)*pageSize,pageSize
    @GetMapping("/querySomeBrand")
    public Result querySomeBrand(String brandName,Integer currentPage){
        return Result.success(productManagerService.querySomeBrand(brandName,currentPage));
    }
    //增加品牌
    @PostMapping("/insertBrand")
    public Result insertBrand(@RequestBody Brand brand){
        return productManagerService.changeBrand(brand,0);
    }
    //修改品牌信息
    @PutMapping("/updateBrand")
    public Result updateBrand(@RequestBody Brand brand){
        return productManagerService.changeBrand(brand,1);
    }
    //删除品牌信息
    @DeleteMapping("/deleteBrand")
    public Result deleteBrand(Integer brandId){
        productManagerService.deleteBrand(brandId);
        return Result.buildSuccess();
    }

    //下拉框列表渲染
    @GetMapping("/getLists")
    public Result getLists(){
        HashMap<String, List<String>> lists = new HashMap<>();
        //虽然数据库中主键不能为空，但只要不让他进入查询就行
        //供货商id
        List<String> supId = productManagerService.getLists("supId", "supplier",null);
        //供货商名单获取
        List<String> supnames = productManagerService.getLists("supName", "supplier",null);
        //品牌商名单获取
        List<String> brands = productManagerService.getLists("brandName", "brand",null);
        //商品类型名单获取
        List<String> types = productManagerService.getLists("productType", "producttype",null);
        //一级商品类型名单获取
        List<String> parenttypes = productManagerService.getLists("productType", "producttype",0);
        //存储数据
        lists.put("supId",supId);
        lists.put("supnames",supnames);
        lists.put("brands",brands);
        lists.put("types",types);
        lists.put("parenttypes",parenttypes);
        //返回数据
        return Result.success(lists);
    }

    //商品信息管理
    //查询
    @PostMapping("/queryProduct")
    public Result queryProduct(@RequestBody ReqQueryProduct reqQueryProduct){
        PageInfo<Product> queryProduct = productManagerService.queryProduct(reqQueryProduct);
//        System.out.println("--------------------------------------");
//        System.out.println(queryProduct.getList().toArray().length);
//        System.out.println(reqQueryProduct);
        if(queryProduct==null || queryProduct.getList().toArray().length==0){//无数据
            return Result.error("无此数据，请重新输入！");
        }
        return Result.success(queryProduct);
    }
    //增
    @PostMapping("/insertProduct")
    public Result insertProduct(@RequestBody ReqQueryProduct reqQueryProduct){
        //code==0:插入
        return productManagerService.changeProduct(reqQueryProduct,0);
    }
    //改
    @PutMapping("/updateProduct")
    public Result updateProduct(@RequestBody ReqQueryProduct reqQueryProduct){
        //code==1:修改
        return productManagerService.changeProduct(reqQueryProduct,1);
    }
    //删
    @DeleteMapping("/deleteProduct")
    public Result deleteProduct(Integer productId){
        productManagerService.deleteProduct(productId);
        return Result.buildSuccess();
    }

    //商品类型管理
    //全查，分别是所有一类（0）和所有二类（!0）
    @GetMapping("/queryTypes")
    public Result queryTypes(Integer parentId,Integer currentPage){
        return Result.success(productManagerService.queryTypes(parentId,currentPage));
    }
    //查特定二类，前端是从一个一类进入的
    @GetMapping("/queryType")
    public Result queryType(Integer parentId,Integer currentPage){
        return Result.success(productManagerService.queryType(parentId,currentPage));
    }
    //增
    //传递参数:新增的子类型名(producttype.productType)、归属的父类型名(productType)
    @PostMapping("/insertType")
    public Result insertType(@RequestBody ReqQueryProduct reqQueryProduct){
        return productManagerService.changeType(reqQueryProduct,0);
    }
    //改
    //需要的参数：父子类型名、子类型id
    @PutMapping("/updateType")
    public Result updateType(@RequestBody ReqQueryProduct reqQueryProduct){
        return productManagerService.changeType(reqQueryProduct,1);
    }
    //删
    @DeleteMapping("/deleteType")
    public Result deleteType(Integer productTypeId){
        productManagerService.deleteType(productTypeId);
        return Result.buildSuccess();
    }

}
